package com.hongxin.nw.netty.factory;

import com.alibaba.fastjson.JSONObject;
import com.hongxin.nw.netty.down.*;
import com.hongxin.nw.netty.model.Transfer;
import com.hongxin.nw.netty.utils.JSONUtils;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;
import lombok.extern.slf4j.Slf4j;

/**
 * @remark 应答确认工厂(type = mrenal)
 * 根据Mrena1的record字段做判断
 * @author: GuoXiaolin
 * @date 2019/5/31 16:41
 * @Version 1.0
 * @Descibe
 */
@Slf4j
public class AnswerFactory {

    public static AnswerFactory getInstance(){
        return new AnswerFactory();
    }

    //获取应答16进制字符串
    public JSONObject answerConfirm(Transfer transfer) throws Exception{
        JSONObject jz = JSONUtils.getRoot(0, StaticValue.VAL_21, transfer.getRecord());
        if (transfer.isNeedRecord()){
            if (TypeIdentif.M_HB.getInt() == transfer.getRecord()){
                //心跳确认
                ChannelRequest cr = new ChannelRequest(new HeartBeatReq());
                jz = cr.execute(transfer);
            }
            if (TypeIdentif.M_RECORD_START_CHARGE.getInt() == transfer.getRecord()){
                //返回 远程启动充电执行结果确认
                ChannelRequest cr = new ChannelRequest(new ChargeConfirmReq());
                jz = cr.execute(transfer);
            }
            if (TypeIdentif.M_RECORD_UPLOAD_DATA.getInt() == transfer.getRecord()){
                //充电记录上传数据确认,,如果“充电记录确认”的成功标识为失败，则需重新上传，最多不超过三次。
                ChannelRequest cr = new ChannelRequest(new ChargeRecordConfirmReq());
                jz = cr.execute(transfer);
            }
            if (TypeIdentif.M_RECORD_CHARGE_PROCESS.getInt() == transfer.getRecord()){
                //充电过程中上传数据确认
                ChannelRequest cr = new ChannelRequest(new ChargeProcessConfirmReq());
                jz = cr.execute(transfer);
            }
            if (TypeIdentif.M_RECORD_RANDOM.getInt() == transfer.getRecord()){
                //交易取随机数下行数据
                ChannelRequest cr = new ChannelRequest(new TranReq());
                jz = cr.execute(transfer);
            }
            if (TypeIdentif.M_RECORD_AUTH.getInt() == transfer.getRecord()){
                // A.10 充电鉴权下行数据
                ChannelRequest cr = new ChannelRequest(new ChargeAuthReq());
                jz = cr.execute(transfer);
            }
            if (TypeIdentif.M_RECORD_BLACK_LIST.getInt() == transfer.getRecord()){
                //A.6黑名单下发时下行数据
                ChannelRequest cr = new ChannelRequest(new BlackListReq());
                jz = cr.execute(transfer);
            }
        }
        return jz;
    }
}
